using System.Data.Entity;
using AdventureWorks.Model.Entities;
using System.Data.Entity.ModelConfiguration.Conventions;

namespace AdventureWorks.DAL.Setup
{
	public class AdventureWorksContext : DbContext
	{
		
		protected override void OnModelCreating(DbModelBuilder modelBuilder)
		{
			modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

			modelBuilder.Entity<Customer>()
				.HasMany(c => c.Addresses)
				.WithMany(i => i.Customers)
				.Map(t => t.MapLeftKey("CustomerID")
					.MapRightKey("AddressID")
				.ToTable("CustomerAddress"));

			modelBuilder.Entity<Product>()
				.HasOptional(t => t.ProductCategory)
				.WithMany(t => t.Products)
				.Map(t => t.MapKey("ProductCategoryID"));

			modelBuilder.Entity<Product>()
				.HasOptional(t => t.ProductModel)
				.WithMany(t => t.Products)
				.Map(t => t.MapKey("ProductModelID"));

			modelBuilder.Entity<ProductCategory>()
				.HasOptional(t => t.productCategory)
				.WithMany(t => t.ProductCategories)
				.Map(t => t.MapKey("ParentProductCategoryID"));
		}

		
	}
}

